import functools
from .models import CallBackLog


def callback_log(request_type):
    """
    :param request_type: 请求类型 get/post
    :param url: 请求地址
    :param request_content: 请求内容
    :return:
    """

    def decorator(func):
        @functools.wraps(func)
        def _wrapped_func(request, *args, **kwargs):
            function_name = func.__name__
            result, url, request_content = func(request, *args, **kwargs)  # 返回结果
            CallBackLog.objects.create(
                url=url,
                content=request_content,
                response_result=result,
                request_type=request_type,
                function_name=function_name
            )
            return result

        return _wrapped_func

    return decorator
